/* 
 * Quechua - the lightweight data mining framework
 *
 * Copyright (C) 2012 Marek Denis <quechua@octogan.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LOG_H
#define	LOG_H

#include <cstdio>
#include <string>
#include <sstream>
#include <string.h>
#include <time.h>

#include "types.h"
#define LOG(loglevel) \
    if(loglevel > Logging::get_level() || ! Logging::get_stream()); \
    else Logging().output(__FILE__,__LINE__,loglevel)

enum  LOGLEVELS  {
        DISABLED = 0,
        INFO,
        MESSAGE,
        WARN,
        ERROR,
        CRITIC,
        DEBUG,
        VERBOSE,
};

LOGLEVELS logname2level(const char* logname);
       

/** Decided to use stack objects after reading this:
 * http://www.drdobbs.com/cpp/logging-in-c/201804215?pgno=1
 **/ 

class Logging {
    
    static const char* const colors[8][2]; 
    static const char* const endmarker;
    
 public:
    Logging(): current_level(CRITIC) {};
    virtual ~Logging();

    std::ostringstream& output(const char* filename, u_int32_t line,
                               LOGLEVELS level);
    std::ostringstream& raw_output(LOGLEVELS level_);

    static LOGLEVELS get_level();
    static void set_level(LOGLEVELS level_);

    static FILE*& get_stream();
    static void redirect_output(const char* path);
    static void close_output();
    static void setcolorize(bool c);
    static bool getcolorize();
    

 protected:
    static LOGLEVELS level;
    LOGLEVELS current_level;
    std::ostringstream os;
   
    Logging(const Logging&);
    Logging& operator=(const Logging&);
    const char* strip(const char* filename);

    static FILE* logging_stream;
    static bool  colorize;

};
#endif	/* LOG_H */
